<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let input = `1 2 5 2
        2 4 4 5
        3 5 7 1
        4 6 2 4`
        let map_list = input.split('\n').map(v => {
            return v.trim().split(' ').map(j => parseInt(j))
        })

        let [M, N] = [map_list.length, map_list[0].length]
        let visited = new Set()
        let max = 0
        let list = []


        for (let i = 0; i < N; i++) {
            for (let j = 0; j < M; j++) {
                if (visited.has(`${i}${j}`)) {
                    continue
                } else {
                    visited.add(`${i}${j}`)
                    list.push([i, j])
                    let count = 1
                    while (list.length) {
                        let [x, y] = list.shift()
                        if (!visited.has(`${x}${y - 1}`) && y - 1 >= 0) {
                            if (Math.abs(map_list[x][y] - map_list[x][y - 1]) <= 1) {
                                list.push([x, y - 1])
                                visited.add(`${x}${y - 1}`)
                                count++
                            }
                        }
                        if (!visited.has(`${x}${y + 1}`) && y + 1 < N) {
                            if (Math.abs(map_list[x][y] - map_list[x][y + 1]) <= 1) {
                                list.push([x, y + 1])
                                visited.add(`${x}${y + 1}`)
                                count++
                            }
                        }
                        if (!visited.has(`${x + 1}${y}`) && x + 1 < M) {
                            if (Math.abs(map_list[x][y] - map_list[x + 1][y]) <= 1) {
                                list.push([x + 1, y])
                                visited.add(`${x + 1}${y}`)
                                count++
                            }
                        }
                        if (!visited.has(`${x - 1}${y}`) && x - 1 >= 0) {
                            if (Math.abs(map_list[x][y] - map_list[x - 1][y]) <= 1) {
                                list.push([x - 1, y])
                                visited.add(`${x - 1}${y}`)
                                count++
                            }
                        }

                    }
                    if (count > max) {
                        max = count
                    }
                }
            }

        }

        console.log(visited);
        console.log(map_list);
        console.log(max);


    </script>
</body>

</html>